<!--conf
<sample>
              <product version="2.0" edition="pro"/>
                     <modifications>
                            <modified date="070101"/>
                     </modifications>
               </sample>
 --> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title>Dynamic context menu</title>
	
<link rel="STYLESHEET" type="text/css" href="../../codebase/dhtmlxgrid.css">
	   
 <link rel="stylesheet" type="text/css" href="../../../dhtmlxMenu/codebase/skins/dhtmlxmenu_standard.css">
  
	<script  src="../../codebase/dhtmlxcommon.js"></script>
	<script  src="../../codebase/dhtmlxgrid.js"></script>
	<script  src="../../codebase/dhtmlxgridcell.js"></script>
	<script  src="../../codebase/excells/dhtmlxgrid_excell_link.js"></script>
	<script language="JavaScript" src="../../../dhtmlxMenu/codebase/dhtmlxmenu.js"></script>

</head>

<body onresize="return true;">
<link rel='STYLESHEET' type='text/css' href='../common/style.css'>
<table cellspacing="0" cellpadding="0" class="sample_header" border="0">
	<tr valign="middle">
		<!-- COMPONENT ICON -->
		<td width="40" align="center"><img src="../common/dhtmlxgrid_icon.gif" border="0"></td>
		<!-- COMPONENT NAME -->
		<td width="150" align="left" nowrap="true">Sample: dhtmlxGrid</td>
		<!-- SAMPLE TITLE -->
		<td width="0" align="left" nowrap><b>Dynamic context menu</b></td>
		<!-- LINK TO COMPONENT PAGE -->
		<td width="0" align="right"><a href="http://www.dhtmlx.com/docs/products/dhtmlxGrid/index.shtml">dhtmlxGrid main page</a></td>
		<!-- CLOSE BUTTON -->
		<td width="50"><div class="sample_close"><a href="javascript:void(0);" onclick="self.close();"><img src="../common/sample_close.gif" width="14" height="14" border="0" alt="X"></a></div></td>
	</tr>
</table>
	
	
    <p>In example below context menu is one for all columns, but it is shown differently by using  hideButtons/showButtons menu methods.</p>
	<table>
		<tr>
			<td>
				<div id="gridbox" width="600px" height="270px" style="background-color:white;"></div>
			</td>
    	</tr>
	</table>


<div class='sample_code'><div class="hl-main"><pre><span class="hl-code"> 
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">script</span><span class="hl-brackets">&gt;</span><span class="hl-code"><div class="hl-main"><pre><span class="hl-code"> 
    </span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">onShowMenu</span><span class="hl-brackets">(</span><span class="hl-identifier">rowId</span><span class="hl-code">,</span><span class="hl-identifier">celInd</span><span class="hl-code">,</span><span class="hl-identifier">grid</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
        ...
        </span><span class="hl-reserved">switch</span><span class="hl-brackets">(</span><span class="hl-identifier">celInd</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-reserved">case</span><span class="hl-code"> </span><span class="hl-number">0</span><span class="hl-code">:
                </span><span class="hl-identifier">menu</span><span class="hl-code">.</span><span class="hl-identifier">showItem</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">inc</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
                </span><span class="hl-identifier">menu</span><span class="hl-code">.</span><span class="hl-identifier">showItem</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">dec</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
                  </span><span class="hl-reserved">break</span><span class="hl-code">;
            ...
            
    </span><span class="hl-brackets">}</span><span class="hl-code"> 
        
    </span><span class="hl-identifier">menu</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">dhtmlXMenuObject</span><span class="hl-brackets">(</span><span class="hl-reserved">null</span><span class="hl-code">,</span><span class="hl-quotes">&quot;</span><span class="hl-string">standard</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    ...
    </span><span class="hl-identifier">mygrid</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">dhtmlXGridObject</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">gridbox</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">mygrid</span><span class="hl-code">.</span><span class="hl-identifier">enableContextMenu</span><span class="hl-brackets">(</span><span class="hl-identifier">menu</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">mygrid</span><span class="hl-code">.</span><span class="hl-identifier">attachEvent</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">onBeforeContextMenu</span><span class="hl-quotes">&quot;</span><span class="hl-code">,</span><span class="hl-identifier">onShowMenu</span><span class="hl-brackets">)</span><span class="hl-code">;
    ...</span></pre></div></span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">script</span><span class="hl-brackets">&gt;</span></pre></div></div><br>

<script>
			function onButtonClick(menuitemId){
			 var data=mygrid.contextID.split("_"); //rowInd_colInd
				var rId = data[0];
				var cInd = data[1];
                switch(menuitemId){
                    case "add":
                        mygrid.addRow((new Date()).valueOf(),["","","","","","","",""],mygrid.getRowIndex(data[0]));
                        break;
                    case "delete":
                        window.setTimeout("mygrid.deleteRow("+rId+");",200)
                        break;
					case "inc":
						var value = parseFloat(mygrid.cells(rId,cInd).getValue())+100;
						mygrid.cells(rId,cInd).setValue(value);
						break;
					case "dec":
						var value = parseFloat(mygrid.cells(rId,cInd).getValue())-200;
						mygrid.cells(rId,cInd).setValue(value);
						break;
					case "link":
						var url = mygrid.getUserData(rId,"link")
						if(url) window.open(url)
						else alert("Page for this author wasn't set")
						break;
					case "edit":
						var rIndex = mygrid.getRowIndex(rId)
						mygrid.selectCell(rIndex,cInd)
						mygrid.editCell();
						break;
					case "set_avail":
						mygrid.cells(rId,cInd).setValue(1);
						break;
					case "set_unavail":
						mygrid.cells(rId,cInd).setValue(0);
						break;
					case "del_best":
						mygrid.cells(rId,cInd).setValue(0);
						break;
					case "add_best":
						mygrid.cells(rId,cInd).setValue(1);
						break;
                }
			}
			
			function onShowMenu(rowId,celInd,grid){
				
				var arr = ["inc","dec","link","edit","set_avail","set_unavail","del_best","add_best"];
				for(var i = 0 ; i < arr.length; i++){
					menu.hideItem(arr[i]);
				}
				switch(celInd){
	
					case 0:
						menu.showItem("inc");
						menu.showItem("dec");
  						break;    
					case 1:
						menu.showItem("link");
  						break;
					case 2:
  						menu.showItem("link");
  						break;
					case 3:
						menu.showItem("edit");
  						break;
					case 4:
						menu.showItem("set_avail");
						menu.showItem("set_unavail");
  						break;
					case 6:
						menu.showItem("del_best");
						menu.showItem("add_best");
  						break;
				}
				return true
			}
			          
			menu = new dhtmlXMenuObject(null,"standard");
	menu.setImagePath("../../../dhtmlxMenu/codebase/imgs/");
	menu.setIconsPath("../images/");
	menu.renderAsContextMenu();
	menu.setOpenMode("web");
	menu.attachEvent("onClick",onButtonClick);
	menu.loadXML("dyn_context.xml");

			mygrid = new dhtmlXGridObject('gridbox');
			mygrid.setImagePath("../../codebase/imgs/");
			mygrid.enableContextMenu(menu);
    		mygrid.init();
			mygrid.attachEvent("onBeforeContextMenu",onShowMenu);
			mygrid.setSkin("modern")
			mygrid.loadXML("gridH.xml");
</script>

<!-- FOOTER -->
<table callspacing="0" cellpadding="0" border="0" class="sample_footer"><tr><td style="padding-left: 8px;">&copy; <a href="http://www.dhtmlx.com">DHTMLX LTD</a>. All rights reserved</td></tr></table>
<!-- FOOTER -->

</body>
</html>
